﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace SIF_2.DataLayer
{
    class DAClientes
    {
        ContabilidadEntities ObjContext = new ContabilidadEntities();
        
      
       

        public void addCustomer(string pName, string pAapellido, string pTelefono, string pCedula, string pDireccion, string pEmail)
        { 
            Clientes Customer = new Clientes();

            Customer.Nombre = pName;
            Customer.Apellido = pAapellido;
            Customer.Telefono = pTelefono;
            Customer.Cedula = pCedula;
            Customer.Direccion = pDireccion;
            Customer.Email = pEmail;


            ObjContext.AddToClientes(Customer);
            ObjContext.SaveChanges();

        
        }

        public void deleteCustomer(string pCedula)
        {
            try
            {
                Clientes Customer = new Clientes();
                Customer = (from p in ObjContext.Clientes
                            where p.Cedula == pCedula
                            select p).First();

                ObjContext.DeleteObject(Customer);
                ObjContext.SaveChanges();
            }
            catch (Exception)
            {
                MessageBox.Show("No existe el cliente");
                
            }
        }

        public Clientes ObtenerClientes(string pCedula)

        {
            try
            {
                Clientes Customer = new Clientes();
                Customer = (from p in ObjContext.Clientes
                            where p.Cedula == pCedula
                            select p).First();


                return Customer;

            }

            catch (Exception)
            {

                throw new System.InvalidOperationException();
              
                
              
            }
 
        }

        public int Codigo { get; set; }
        public string Nombre { get; set; }
        public string Apellido { get; set; }
        public string Telefono { get; set; }
        public string Cedula { get; set; }
        public string Direccion { get; set; }
        public string Email { get; set; }

        public IQueryable<DAClientes> getClientes()
        {

            var Resultado = (from c in ObjContext.Clientes
                             select new DAClientes

                             {
                                 Codigo = c.Id,
                                 Nombre = c.Nombre,
                                 Apellido = c.Apellido,
                                 Telefono = c.Telefono,
                                 Cedula = c.Cedula,
                                 Direccion = c.Direccion,
                                 Email = c.Email

                             });

            return Resultado.AsQueryable();


        }
     

    }
}
